def findCircleNum(isConnected):
    """
    :type isConnected: List[List[int]]
    :rtype: int
    """
    n = len(isConnected)
    num = 0
    visit = set()
    for i in range(n):
        if i not in visit:
            dfs(isConnected, n, i, visit)
            num += 1
    return num


def dfs(isConnected, n, i, visit):
    for j in range(n):
        if isConnected[i][j] == 1 and j not in visit:
            visit.add(j)
            dfs(isConnected, n, j, visit)


isConnected = [[1, 0, 0, 1], [0, 1, 1, 0], [0, 1, 1, 1], [1, 0, 1, 1]]
print(findCircleNum(isConnected))  # 1
